﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RaGE_JD.Properties;

namespace RaGE_JD
{
    public partial class MainWindow
    {
        private void InitResDB()
        {
            resDB = new SortedList();

            if (!File.Exists(Application.StartupPath + "\\restoname.txt")) return;

            try
            {
                var streamReader = new StreamReader(Application.StartupPath + "\\restoname.txt", Encoding.Unicode);
                var separator = new[] { '\t' };

                while (true)
                {
                    var text = streamReader.ReadLine();

                    if (text == null)
                        break;

                    if (!text.Contains("\t")) continue;

                    var array = text.Split(separator);
                    resDB.Add(array[0], array[1]);
                }
                streamReader.Close();
            }
            catch (Exception initResDBEx)
            {
                MessageBox.Show(Resources.ErrReadResTbl + initResDBEx.Message);
            }
        }

        private void InitNPCDB()
        {
            npcDB = new SortedList();
            npcID = new List<SortedList>();
            npcID.Add(new SortedList());

            if (!File.Exists(Application.StartupPath + "\\idtoname.txt")) return;

            try
            {
                var streamReader = new StreamReader(Application.StartupPath + "\\idtoname.txt", Encoding.Unicode);
                var separator = new[] { '\t' };

                while (streamReader.Peek() >= 0)
                {
                    var text = streamReader.ReadLine();

                    if (text != null && !text.Contains("\t")) continue;

                    if (text == null) continue;

                    var array = text.Split(separator);
                    npcDB.Add(array[0], array[1]);

                    for (var i = 0; i < npcID.Count; i++)
                    {
                        if (!npcID[i].ContainsKey(array[1]))
                        {
                            npcID[i].Add(array[1], array[0]);
                            break;
                        }

                        if (i != npcID.Count - 1) continue;

                        npcID.Add(new SortedList());
                        npcID[i + 1].Add(array[1], array[0]);
                        break;
                    }
                }
                streamReader.Close();
            }
            catch (Exception initNPCDBEx)
            {
                MessageBox.Show(Resources.ErrReadDataTbl + initNPCDBEx.Message);
            }
        }

        private void InitNPCGen()
        {
            InitResDB();
            InitNPCDB();
            var initPath = Application.StartupPath + "\\License.sys";

            if (File.Exists(initPath))
            {
                var fileStream = File.OpenRead(initPath);
                var num = (int)fileStream.Length;
                var array = new byte[num];
                var encoding = Encoding.GetEncoding("GB2312");

                fileStream.Read(array, 0, array.Length);

                var @string = encoding.GetString(array);
                RichText.Text = @string;
                fileStream.Close();
            }

            getXYZ = new GetXYZ();

            foreach (var offset in getXYZ.clientOffsets.Cast<Offset>())
                comboAsstClient.Items.Add(offset.Version);

            if (comboAsstClient.Items.Count > 0)
                comboAsstClient.SelectedIndex = 0;
        }

        private string NPCName(int id)
        {
            var result = (string)npcDB[id.ToString(CultureInfo.InvariantCulture)];
            return result ?? ("Not found...");
        }

        private void DrawCreatures(IList<CreatureSets> cs)
        {
            gridCreatureGrps.Rows.Clear();
            gridCreatures.Rows.Clear();

            for (var i = 0; i < cs.Count; i++)
            {
                var array = new string[22];
                var creatureGroupsCount = cs[i].creature_groups_count;

                array[0] = creatureGroupsCount.ToString(CultureInfo.InvariantCulture);
                array[1] = Column2.Items[cs[i].spawn_mode].ToString();

                var spawnX = cs[i].spawn_x;
                array[2] = spawnX.ToString("F3");

                var spawnAlt = cs[i].spawn_alt;
                array[3] = spawnAlt.ToString("F3");

                var spawnZ = cs[i].spawn_z;
                array[4] = spawnZ.ToString("F3");

                var rot = cs[i].rot_1;
                array[5] = rot.ToString("F3");

                var rot2 = cs[i].rot_2;
                array[6] = rot2.ToString("F3");

                var rot3 = cs[i].rot_3;
                array[7] = rot3.ToString("F3");

                var spreadX = cs[i].spread_x;
                array[8] = spreadX.ToString("F3");

                var spreadAlt = cs[i].spread_alt;
                array[9] = spreadAlt.ToString("F3");

                var spreadZ = cs[i].spread_z;
                array[10] = spreadZ.ToString("F3");
                array[11] = Column12.Items[cs[i].unknown_7].ToString();

                var unknown = cs[i].unknown_8;
                array[12] = unknown.ToString(CultureInfo.InvariantCulture);

                var unknown2 = cs[i].unknown_9;
                array[13] = unknown2.ToString();

                var unknown3 = cs[i].unknown_10;
                array[14] = unknown3.ToString();

                var unknown4 = cs[i].unknown_11;
                array[15] = unknown4.ToString();

                var unknown5 = cs[i].unknown_12;
                array[16] = unknown5.ToString(CultureInfo.InvariantCulture);

                var trigger = cs[i].trigger;
                array[17] = trigger.ToString(CultureInfo.InvariantCulture);

                var unknown6 = cs[i].unknown_14;
                array[18] = unknown6.ToString(CultureInfo.InvariantCulture);

                var unknown7 = cs[i].unknown_15;
                array[19] = unknown7.ToString(CultureInfo.InvariantCulture);

                var unknown8 = cs[i].unknown_16;
                array[20] = unknown8.ToString(CultureInfo.InvariantCulture);

                var unknown9 = cs[i].unknown_17;
                array[21] = unknown9.ToString(CultureInfo.InvariantCulture);

                // ReSharper disable CoVariantArrayConversion
                gridCreatures.Rows.Add(array);
                // ReSharper restore CoVariantArrayConversion

                if (cs[i].creature_groups_count <= 0) continue;

                var id = cs[i].creature_groups[0].id;

                gridCreatures.Rows[i].HeaderCell.Value =
                    "[" + id.ToString(CultureInfo.InvariantCulture) + "] " + NPCName(cs[i].creature_groups[0].id);
            }

        }

        private void JDLoadCreatures(BinaryReader br)
        {
            gridCreatures.Rows.Clear();

            npcgen.creature_sets = new CreatureSets[npcgen.creature_sets_count];

            for (var i = 0; i < npcgen.creature_sets.Length; i++)
            {
                npcgen.creature_sets[i] = new CreatureSets();
                npcgen.creature_sets[i].spawn_mode = br.ReadInt32();
                npcgen.creature_sets[i].creature_groups_count = br.ReadInt32();
                npcgen.creature_sets[i].spawn_x = br.ReadSingle();
                npcgen.creature_sets[i].spawn_alt = br.ReadSingle();
                npcgen.creature_sets[i].spawn_z = br.ReadSingle();
                npcgen.creature_sets[i].rot_1 = br.ReadSingle();
                npcgen.creature_sets[i].rot_2 = br.ReadSingle();
                npcgen.creature_sets[i].rot_3 = br.ReadSingle();
                npcgen.creature_sets[i].spread_x = br.ReadSingle();
                npcgen.creature_sets[i].spread_alt = br.ReadSingle();
                npcgen.creature_sets[i].spread_z = br.ReadSingle();
                npcgen.creature_sets[i].unknown_7 = br.ReadInt32();
                npcgen.creature_sets[i].unknown_8 = br.ReadInt32();
                npcgen.creature_sets[i].unknown_9 = br.ReadBoolean();
                npcgen.creature_sets[i].unknown_10 = br.ReadBoolean();
                npcgen.creature_sets[i].unknown_11 = br.ReadBoolean();
                npcgen.creature_sets[i].unknown_12 = br.ReadInt32();
                npcgen.creature_sets[i].trigger = br.ReadInt32();
                npcgen.creature_sets[i].unknown_14 = br.ReadInt32();
                npcgen.creature_sets[i].unknown_15 = br.ReadInt32();
                npcgen.creature_sets[i].unknown_16 = br.ReadInt32();
                npcgen.creature_sets[i].unknown_17 = br.ReadInt32();

                npcgen.creature_sets[i].creature_groups = new CreatureGroup[npcgen.creature_sets[i].creature_groups_count];

                for (var j = 0; j < npcgen.creature_sets[i].creature_groups.Length; j++)
                {
                    npcgen.creature_sets[i].creature_groups[j] = new CreatureGroup();

                    npcgen.creature_sets[i].creature_groups[j].id = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].amount = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].respawn = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_2 = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_3 = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_4 = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_5 = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_6 = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_7 = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_8 = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_9 = br.ReadBoolean();
                    npcgen.creature_sets[i].creature_groups[j].unknown_10 = br.ReadBoolean();
                    npcgen.creature_sets[i].creature_groups[j].unknown_11 = br.ReadBoolean();
                    npcgen.creature_sets[i].creature_groups[j].unknown_12 = br.ReadBoolean();
                    npcgen.creature_sets[i].creature_groups[j].unknown_13 = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_14 = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_15 = br.ReadInt32();
                    npcgen.creature_sets[i].creature_groups[j].unknown_16 = br.ReadInt32();
                }

                if (npcgen.creature_sets[i].unknown_17 <= 0) continue;

                npcgen.creature_sets[i].mutualExclusionGroup = new int[npcgen.creature_sets[i].unknown_17];

                for (var k = 0; k < npcgen.creature_sets[i].unknown_17; k++)
                    npcgen.creature_sets[i].mutualExclusionGroup[k] = br.ReadInt32();
            }
            DrawCreatures(npcgen.creature_sets);
        }

        private string ResName(int id)
        {
            var num = id;
            var text = (string)resDB[num.ToString(CultureInfo.InvariantCulture)] ?? "Not found...";
            return text;
        }

        private void DrawResources(IList<ResourceSets> cs)
        {
            gridResources.Rows.Clear();

            for (var i = 0; i < cs.Count; i++)
            {
                var array = new string[17];
                var resourceGroupsCount = cs[i].resource_groups_count;
                array[0] = resourceGroupsCount.ToString(CultureInfo.InvariantCulture);

                var spawnX = cs[i].spawn_x;
                array[1] = spawnX.ToString("F3");

                var spawnAlt = cs[i].spawn_alt;
                array[2] = spawnAlt.ToString("F3");

                var spawnZ = cs[i].spawn_z;
                array[3] = spawnZ.ToString("F3");

                var spreadX = cs[i].spread_x;
                array[4] = spreadX.ToString("F3");

                var spreadZ = cs[i].spread_z;
                array[5] = spreadZ.ToString("F3");

                var unknown = cs[i].unknown_1;
                array[6] = unknown.ToString();

                var unknown2 = cs[i].unknown_2;
                array[7] = unknown2.ToString();

                var unknown3 = cs[i].unknown_3;
                array[8] = unknown3.ToString();

                var unknown4 = cs[i].unknown_4;
                array[9] = unknown4.ToString(CultureInfo.InvariantCulture);

                var unknown5 = cs[i].unknown_5;
                array[10] = unknown5.ToString(CultureInfo.InvariantCulture);

                var unknown6 = cs[i].unknown_6;
                array[11] = unknown6.ToString(CultureInfo.InvariantCulture);

                var unknown7 = cs[i].unknown_7;
                array[12] = unknown7.ToString(CultureInfo.InvariantCulture);

                var unknownTrigger = cs[i].unknown_trigger;
                array[13] = unknownTrigger.ToString(CultureInfo.InvariantCulture);

                var unknown8 = cs[i].unknown_8;
                array[14] = unknown8.ToString(CultureInfo.InvariantCulture);

                var unknownIndex = cs[i].unknown_index;
                array[15] = unknownIndex.ToString(CultureInfo.InvariantCulture);

                var unknownMutual = cs[i].unknown_mutual;
                array[16] = unknownMutual.ToString(CultureInfo.InvariantCulture);

                // ReSharper disable CoVariantArrayConversion
                gridResources.Rows.Add(array);
                // ReSharper restore CoVariantArrayConversion

                if (cs[i].resource_groups_count <= 0) continue;

                var id = cs[i].resource_groups[0].id;
                gridResources.Rows[i].HeaderCell.Value =
                    "[" + id.ToString(CultureInfo.InvariantCulture) + "] " + ResName(cs[i].resource_groups[0].id);
            }
        }

        private void JDLoadResources(BinaryReader br)
        {
            npcgen.resource_sets = new ResourceSets[npcgen.resource_sets_count];

            for (var i = 0; i < npcgen.resource_sets.Length; i++)
            {
                npcgen.resource_sets[i] = new ResourceSets();

                npcgen.resource_sets[i].spawn_x = br.ReadSingle();
                npcgen.resource_sets[i].spawn_alt = br.ReadSingle();
                npcgen.resource_sets[i].spawn_z = br.ReadSingle();
                npcgen.resource_sets[i].spread_x = br.ReadSingle();
                npcgen.resource_sets[i].spread_z = br.ReadSingle();
                npcgen.resource_sets[i].resource_groups_count = br.ReadInt32();
                npcgen.resource_sets[i].unknown_1 = br.ReadBoolean();
                npcgen.resource_sets[i].unknown_2 = br.ReadBoolean();
                npcgen.resource_sets[i].unknown_3 = br.ReadBoolean();
                npcgen.resource_sets[i].unknown_4 = br.ReadInt32();
                npcgen.resource_sets[i].unknown_5 = br.ReadByte();
                npcgen.resource_sets[i].unknown_6 = br.ReadByte();
                npcgen.resource_sets[i].unknown_7 = br.ReadByte();
                npcgen.resource_sets[i].unknown_trigger = br.ReadInt32();
                npcgen.resource_sets[i].unknown_8 = br.ReadInt32();
                npcgen.resource_sets[i].unknown_index = br.ReadInt32();
                npcgen.resource_sets[i].unknown_mutual = br.ReadInt32();

                npcgen.resource_sets[i].resource_groups = new ResourceGroup[npcgen.resource_sets[i].resource_groups_count];

                for (var j = 0; j < npcgen.resource_sets[i].resource_groups.Length; j++)
                {
                    npcgen.resource_sets[i].resource_groups[j] = new ResourceGroup();

                    npcgen.resource_sets[i].resource_groups[j].unknown_1 = br.ReadInt32();
                    npcgen.resource_sets[i].resource_groups[j].id = br.ReadInt32();
                    npcgen.resource_sets[i].resource_groups[j].respawn = br.ReadInt32();
                    npcgen.resource_sets[i].resource_groups[j].amount = br.ReadInt32();
                    npcgen.resource_sets[i].resource_groups[j].unknown_2 = br.ReadInt32();
                }

                if (npcgen.resource_sets[i].unknown_mutual <= 0) continue;

                npcgen.resource_sets[i].mutualExclusionGroup = new int[npcgen.resource_sets[i].unknown_mutual];

                for (var k = 0; k < npcgen.resource_sets[i].unknown_mutual; k++)
                    npcgen.resource_sets[i].mutualExclusionGroup[k] = br.ReadInt32();
            }

            DrawResources(npcgen.resource_sets);
        }

        private void JDDrawDynamics(IList<Dynamic> cs)
        {
            gridDynamics.Rows.Clear();

            for (var i = 0; i < cs.Count; i++)
            {
                var array = new string[9];
                var id = cs[i].id;
                array[0] = id.ToString(CultureInfo.InvariantCulture);

                var spawnX = cs[i].spawn_x;
                array[1] = spawnX.ToString("F3");

                var spawnAlt = cs[i].spawn_alt;
                array[2] = spawnAlt.ToString("F3");

                var spawnZ = cs[i].spawn_z;
                array[3] = spawnZ.ToString("F3");

                var unknown = cs[i].unknown_5;
                array[4] = unknown.ToString(CultureInfo.InvariantCulture);

                var unknown2 = cs[i].unknown_6;
                array[5] = unknown2.ToString(CultureInfo.InvariantCulture);

                var unknown3 = cs[i].unknown_7;
                array[6] = unknown3.ToString(CultureInfo.InvariantCulture);

                var unknown4 = cs[i].unknown_8;
                array[7] = unknown4.ToString(CultureInfo.InvariantCulture);

                var trigger = cs[i].trigger;
                array[8] = trigger.ToString(CultureInfo.InvariantCulture);

                // ReSharper disable CoVariantArrayConversion
                gridDynamics.Rows.Add(array);
                // ReSharper restore CoVariantArrayConversion

                var num = gridDynamics.Rows.Count - 1;
                gridDynamics.Rows[i].HeaderCell.Value = num.ToString(CultureInfo.InvariantCulture);
            }
        }

        private void JDLoadDynamics(BinaryReader br)
        {
            gridDynamics.Rows.Clear();
            npcgen.dynamics = new Dynamic[npcgen.dynamics_count];

            for (var i = 0; i < npcgen.dynamics.Length; i++)
            {
                npcgen.dynamics[i] = new Dynamic();
                npcgen.dynamics[i].id = br.ReadInt32();
                npcgen.dynamics[i].spawn_x = br.ReadSingle();
                npcgen.dynamics[i].spawn_alt = br.ReadSingle();
                npcgen.dynamics[i].spawn_z = br.ReadSingle();
                npcgen.dynamics[i].unknown_5 = br.ReadByte();
                npcgen.dynamics[i].unknown_6 = br.ReadByte();
                npcgen.dynamics[i].unknown_7 = br.ReadByte();
                npcgen.dynamics[i].unknown_8 = br.ReadByte();
                npcgen.dynamics[i].trigger = br.ReadInt32();
            }

            JDDrawDynamics(npcgen.dynamics);
        }

        private void JDDrawTriggers(IList<Trigger> cs)
        {
            gridTriggers.Rows.Clear();
            var encoding = Encoding.GetEncoding("GBK");

            for (var i = 0; i < cs.Count; i++)
            {
                var array = new string[23];

                var unknown = cs[i].unknown_1;
                array[0] = unknown.ToString(CultureInfo.InvariantCulture);

                var unknown2 = cs[i].unknown_2;
                array[1] = unknown2.ToString(CultureInfo.InvariantCulture);
                array[2] = encoding.GetString(cs[i].name);

                var unknown3 = cs[i].unknown_3;
                array[3] = unknown3.ToString();

                var unknown4 = cs[i].unknown_4;
                array[4] = unknown4.ToString(CultureInfo.InvariantCulture);

                var unknown5 = cs[i].unknown_5;
                array[5] = unknown5.ToString(CultureInfo.InvariantCulture);

                var unknown6 = cs[i].unknown_6;
                array[6] = unknown6.ToString();

                var unknown7 = cs[i].unknown_7;
                array[7] = unknown7.ToString();

                var year = cs[i].year_1;
                array[8] = year.ToString(CultureInfo.InvariantCulture);

                var month = cs[i].month_1;
                array[9] = month.ToString(CultureInfo.InvariantCulture);
                array[10] = Column44.Items[cs[i].week_day_1 + 1].ToString();

                var day = cs[i].day_1;
                array[11] = day.ToString(CultureInfo.InvariantCulture);

                var hour = cs[i].hour_1;
                array[12] = hour.ToString(CultureInfo.InvariantCulture);

                var minute = cs[i].minute_1;
                array[13] = minute.ToString(CultureInfo.InvariantCulture);

                var year2 = cs[i].year_2;
                array[14] = year2.ToString(CultureInfo.InvariantCulture);

                var month2 = cs[i].month_2;
                array[15] = month2.ToString(CultureInfo.InvariantCulture);
                array[16] = Column48.Items[cs[i].week_day_2 + 1].ToString();

                var day2 = cs[i].day_2;
                array[17] = day2.ToString(CultureInfo.InvariantCulture);

                var hour2 = cs[i].hour_2;
                array[18] = hour2.ToString(CultureInfo.InvariantCulture);

                var minute2 = cs[i].minute_2;
                array[19] = minute2.ToString(CultureInfo.InvariantCulture);

                var duration = cs[i].duration;
                array[20] = duration.ToString(CultureInfo.InvariantCulture);

                var forcedStop = cs[i].forced_stop;
                array[21] = forcedStop.ToString();

                //var unknown8 = cs[i].unknown_8;
                //array[22] = unknown8.ToString();

                // ReSharper disable CoVariantArrayConversion
                gridTriggers.Rows.Add(array);
                // ReSharper restore CoVariantArrayConversion

                var num = gridTriggers.Rows.Count - 1;

                gridTriggers.Rows[i].HeaderCell.Value = num.ToString(CultureInfo.InvariantCulture);
            }
        }

        private void JDLoadTriggers(BinaryReader br)
        {
            gridTriggers.Rows.Clear();
            npcgen.triggers = new Trigger[npcgen.triggers_count];

            for (var i = 0; i < npcgen.triggers.Length; i++)
            {
                npcgen.triggers[i] = new Trigger();
                npcgen.triggers[i].unknown_1 = br.ReadInt32();
                npcgen.triggers[i].unknown_2 = br.ReadInt32();
                npcgen.triggers[i].name = br.ReadBytes(128);
                npcgen.triggers[i].unknown_3 = br.ReadBoolean();
                npcgen.triggers[i].unknown_4 = br.ReadInt32();
                npcgen.triggers[i].unknown_5 = br.ReadInt32();
                npcgen.triggers[i].unknown_6 = br.ReadBoolean();
                npcgen.triggers[i].unknown_7 = br.ReadBoolean();
                npcgen.triggers[i].year_1 = br.ReadInt32();
                npcgen.triggers[i].month_1 = br.ReadInt32();
                npcgen.triggers[i].week_day_1 = br.ReadInt32();
                npcgen.triggers[i].day_1 = br.ReadInt32();
                npcgen.triggers[i].hour_1 = br.ReadInt32();
                npcgen.triggers[i].minute_1 = br.ReadInt32();
                npcgen.triggers[i].year_2 = br.ReadInt32();
                npcgen.triggers[i].month_2 = br.ReadInt32();
                npcgen.triggers[i].week_day_2 = br.ReadInt32();
                npcgen.triggers[i].day_2 = br.ReadInt32();
                npcgen.triggers[i].hour_2 = br.ReadInt32();
                npcgen.triggers[i].minute_2 = br.ReadInt32();
                npcgen.triggers[i].duration = br.ReadInt32();
                npcgen.triggers[i].forced_stop = br.ReadBoolean();
                //npcgen.triggers[i].unknown_8 = br.ReadBoolean();
            }

            JDDrawTriggers(npcgen.triggers);
        }

        private void SaveCreatures(BinaryWriter bw)
        {
            foreach (var t in npcgen.creature_sets)
            {
                bw.Write(t.spawn_mode);
                bw.Write(t.creature_groups_count);
                bw.Write(t.spawn_x);
                bw.Write(t.spawn_alt);
                bw.Write(t.spawn_z);
                bw.Write(t.rot_1);
                bw.Write(t.rot_2);
                bw.Write(t.rot_3);
                bw.Write(t.spread_x);
                bw.Write(t.spread_alt);
                bw.Write(t.spread_z);
                bw.Write(t.unknown_7);
                bw.Write(t.unknown_8);
                bw.Write(t.unknown_9);
                bw.Write(t.unknown_10);
                bw.Write(t.unknown_11);
                bw.Write(t.unknown_12);
                bw.Write(t.trigger);
                bw.Write(t.unknown_14);
                bw.Write(t.unknown_15);
                bw.Write(t.unknown_16);
                bw.Write(t.unknown_17);

                foreach (var u in t.creature_groups)
                {
                    bw.Write(u.id);
                    bw.Write(u.amount);
                    bw.Write(u.respawn);
                    bw.Write(u.unknown_2);
                    bw.Write(u.unknown_3);
                    bw.Write(u.unknown_4);
                    bw.Write(u.unknown_5);
                    bw.Write(u.unknown_6);
                    bw.Write(u.unknown_7);
                    bw.Write(u.unknown_8);
                    bw.Write(u.unknown_9);
                    bw.Write(u.unknown_10);
                    bw.Write(u.unknown_11);
                    bw.Write(u.unknown_12);
                    bw.Write(u.unknown_13);
                    bw.Write(u.unknown_14);
                    bw.Write(u.unknown_15);
                    bw.Write(u.unknown_16);
                }

                if (t.unknown_17 <= 0) continue;

                for (var k = 0; k < t.unknown_17; k++)
                    bw.Write(t.mutualExclusionGroup[k]);
            }
        }

        private void SaveResources(BinaryWriter bw)
        {
            foreach (var t in npcgen.resource_sets)
            {
                bw.Write(t.spawn_x);
                bw.Write(t.spawn_alt);
                bw.Write(t.spawn_z);
                bw.Write(t.spread_x);
                bw.Write(t.spread_z);
                bw.Write(t.resource_groups_count);
                bw.Write(t.unknown_1);
                bw.Write(t.unknown_2);
                bw.Write(t.unknown_3);
                bw.Write(t.unknown_4);
                bw.Write(t.unknown_5);
                bw.Write(t.unknown_6);
                bw.Write(t.unknown_7);
                bw.Write(t.unknown_trigger);
                bw.Write(t.unknown_8);
                bw.Write(t.unknown_index);
                bw.Write(t.unknown_mutual);

                foreach (var u in t.resource_groups)
                {
                    bw.Write(u.unknown_1);
                    bw.Write(u.id);
                    bw.Write(u.respawn);
                    bw.Write(u.amount);
                    bw.Write(u.unknown_2);
                }

                if (t.unknown_mutual <= 0) continue;

                for (var k = 0; k < t.unknown_mutual; k++)
                    bw.Write(t.mutualExclusionGroup[k]);
            }
        }

        private void SaveDynamics(BinaryWriter bw)
        {
            foreach (var t in npcgen.dynamics)
            {
                bw.Write(t.id);
                bw.Write(t.spawn_x);
                bw.Write(t.spawn_alt);
                bw.Write(t.spawn_z);
                bw.Write(t.unknown_5);
                bw.Write(t.unknown_6);
                bw.Write(t.unknown_7);
                bw.Write(t.unknown_8);
                bw.Write(t.trigger);
            }
        }

        private void SaveTriggers(BinaryWriter bw)
        {
            foreach (var t in npcgen.triggers)
            {
                bw.Write(t.unknown_1);
                bw.Write(t.unknown_2);
                bw.Write(t.name);
                bw.Write(t.unknown_3);
                bw.Write(t.unknown_4);
                bw.Write(t.unknown_5);
                bw.Write(t.unknown_6);
                bw.Write(t.unknown_7);
                bw.Write(t.year_1);
                bw.Write(t.month_1);
                bw.Write(t.week_day_1);
                bw.Write(t.day_1);
                bw.Write(t.hour_1);
                bw.Write(t.minute_1);
                bw.Write(t.year_2);
                bw.Write(t.month_2);
                bw.Write(t.week_day_2);
                bw.Write(t.day_2);
                bw.Write(t.hour_2);
                bw.Write(t.minute_2);
                bw.Write(t.duration);
                bw.Write(t.forced_stop);
            }
        }

        private void ChangeCreatures(object sender, DataGridViewCellEventArgs e)
        {
            if (npcgen == null || gridCreatures.CurrentCell == null) return;

            try
            {
                var r = gridCreatures.CurrentCell.RowIndex;

                switch (gridCreatures.CurrentCell.ColumnIndex)
                {
                    case 1:
                        npcgen.creature_sets[r].spawn_mode = Column2.Items.IndexOf(gridCreatures.CurrentCell.Value);
                        break;

                    case 2:
                        npcgen.creature_sets[r].spawn_x = Convert.ToSingle(gridCreatures.CurrentCell.Value);
                        break;

                    case 3:
                        npcgen.creature_sets[r].spawn_alt = Convert.ToSingle(gridCreatures.CurrentCell.Value);
                        break;

                    case 4:
                        npcgen.creature_sets[r].spawn_z = Convert.ToSingle(gridCreatures.CurrentCell.Value);
                        break;

                    case 5:
                        npcgen.creature_sets[r].rot_1 = Convert.ToSingle(gridCreatures.CurrentCell.Value);
                        break;

                    case 6:
                        npcgen.creature_sets[r].rot_2 = Convert.ToSingle(gridCreatures.CurrentCell.Value);
                        break;

                    case 7:
                        npcgen.creature_sets[r].rot_3 = Convert.ToSingle(gridCreatures.CurrentCell.Value);
                        break;

                    case 8:
                        npcgen.creature_sets[r].spread_x = Convert.ToSingle(gridCreatures.CurrentCell.Value);
                        break;

                    case 9:
                        npcgen.creature_sets[r].spread_alt = Convert.ToSingle(gridCreatures.CurrentCell.Value);
                        break;

                    case 10:
                        npcgen.creature_sets[r].spread_z = Convert.ToSingle(gridCreatures.CurrentCell.Value);
                        break;

                    case 11:
                        npcgen.creature_sets[r].unknown_7 = Column12.Items.IndexOf(gridCreatures.CurrentCell.Value);
                        break;

                    case 12:
                        npcgen.creature_sets[r].unknown_8 = Convert.ToInt32(gridCreatures.CurrentCell.Value);
                        break;

                    case 13:
                        npcgen.creature_sets[r].unknown_9 = Convert.ToBoolean(gridCreatures.CurrentCell.Value);
                        break;

                    case 14:
                        npcgen.creature_sets[r].unknown_10 = Convert.ToBoolean(gridCreatures.CurrentCell.Value);
                        break;

                    case 15:
                        npcgen.creature_sets[r].unknown_11 = Convert.ToBoolean(gridCreatures.CurrentCell.Value);
                        break;

                    case 16:
                        npcgen.creature_sets[r].unknown_12 = Convert.ToInt32(gridCreatures.CurrentCell.Value);
                        break;

                    case 17:
                        npcgen.creature_sets[r].trigger = Convert.ToInt32(gridCreatures.CurrentCell.Value);
                        break;

                    case 18:
                        npcgen.creature_sets[r].unknown_14 = Convert.ToInt32(gridCreatures.CurrentCell.Value);
                        break;

                    case 19:
                        npcgen.creature_sets[r].unknown_15 = Convert.ToInt32(gridCreatures.CurrentCell.Value);
                        break;

                    case 20:
                        npcgen.creature_sets[r].unknown_16 = Convert.ToInt32(gridCreatures.CurrentCell.Value);
                        break;

                    case 21:
                        npcgen.creature_sets[r].unknown_17 = Convert.ToInt32(gridCreatures.CurrentCell.Value);
                        break;
                }
            }
            catch
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void ChangeCreatureGroups(object sender, DataGridViewCellEventArgs e)
        {
            if (npcgen == null || gridCreatures.CurrentCell == null ||
                gridCreatureGrps.CurrentCell == null) return;

            try
            {
                var r = gridCreatures.CurrentCell.RowIndex;
                var n = gridCreatureGrps.CurrentCell.RowIndex;

                switch (gridCreatureGrps.CurrentCell.ColumnIndex)
                {
                    case 0:
                        npcgen.creature_sets[r].creature_groups[n].id = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        if (n == 0)
                        {
                            gridCreatures.Rows[r].HeaderCell.Value =
                                "[" + npcgen.creature_sets[r].creature_groups[0].id.ToString(CultureInfo.InvariantCulture) +
                                "] " + NPCName(npcgen.creature_sets[r].creature_groups[0].id);
                        }
                        gridCreatureGrps.Rows[n].HeaderCell.Value = NPCName(npcgen.creature_sets[r].creature_groups[n].id);
                        break;

                    case 1:
                        npcgen.creature_sets[r].creature_groups[n].amount = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 2:
                        npcgen.creature_sets[r].creature_groups[n].respawn = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 3:
                        npcgen.creature_sets[r].creature_groups[n].unknown_2 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 4:
                        npcgen.creature_sets[r].creature_groups[n].unknown_3 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 5:
                        npcgen.creature_sets[r].creature_groups[n].unknown_4 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 6:
                        npcgen.creature_sets[r].creature_groups[n].unknown_5 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 7:
                        npcgen.creature_sets[r].creature_groups[n].unknown_6 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 8:
                        npcgen.creature_sets[r].creature_groups[n].unknown_7 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 9:
                        npcgen.creature_sets[r].creature_groups[n].unknown_8 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 10:
                        npcgen.creature_sets[r].creature_groups[n].unknown_9 = Convert.ToBoolean(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 11:
                        npcgen.creature_sets[r].creature_groups[n].unknown_10 = Convert.ToBoolean(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 12:
                        npcgen.creature_sets[r].creature_groups[n].unknown_11 = Convert.ToBoolean(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 13:
                        npcgen.creature_sets[r].creature_groups[n].unknown_12 = Convert.ToBoolean(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 14:
                        npcgen.creature_sets[r].creature_groups[n].unknown_13 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 15:
                        npcgen.creature_sets[r].creature_groups[n].unknown_14 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 16:
                        npcgen.creature_sets[r].creature_groups[n].unknown_15 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;

                    case 17:
                        npcgen.creature_sets[r].creature_groups[n].unknown_16 = Convert.ToInt32(gridCreatureGrps.CurrentCell.Value);
                        break;
                }
            }
            catch
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void ClickRowCreature(object sender, DataGridViewCellEventArgs e)
        {
            gridCreatureGrps.Rows.Clear();
            gridCreatureGrps.ContextMenuStrip = null;

            if (npcgen == null || e.RowIndex <= -1 || e.RowIndex >= npcgen.creature_sets.Length) return;

            for (var i = 0; i < npcgen.creature_sets[e.RowIndex].creature_groups_count; i++)
            {
                var array = new object[18];
                var id = npcgen.creature_sets[e.RowIndex].creature_groups[i].id;
                array[0] = id.ToString(CultureInfo.InvariantCulture);

                var amount = npcgen.creature_sets[e.RowIndex].creature_groups[i].amount;
                array[1] = amount.ToString(CultureInfo.InvariantCulture);

                var respawn = npcgen.creature_sets[e.RowIndex].creature_groups[i].respawn;
                array[2] = respawn.ToString(CultureInfo.InvariantCulture);

                var unknown = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_2;
                array[3] = unknown.ToString(CultureInfo.InvariantCulture);

                var unknown2 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_3;
                array[4] = unknown2.ToString(CultureInfo.InvariantCulture);

                var unknown3 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_4;
                array[5] = unknown3.ToString(CultureInfo.InvariantCulture);

                var unknown4 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_5;
                array[6] = unknown4.ToString(CultureInfo.InvariantCulture);

                var unknown5 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_6;
                array[7] = unknown5.ToString(CultureInfo.InvariantCulture);

                var unknown6 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_7;
                array[8] = unknown6.ToString(CultureInfo.InvariantCulture);

                var unknown7 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_8;
                array[9] = unknown7.ToString(CultureInfo.InvariantCulture);

                var unknown8 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_9;
                array[10] = unknown8.ToString(CultureInfo.InvariantCulture);

                var unknown9 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_10;
                array[11] = unknown9.ToString(CultureInfo.InvariantCulture);

                var unknown10 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_11;
                array[12] = unknown10.ToString(CultureInfo.InvariantCulture);

                var unknown11 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_12;
                array[13] = unknown11.ToString(CultureInfo.InvariantCulture);

                var unknown12 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_13;
                array[14] = unknown12.ToString(CultureInfo.InvariantCulture);

                var unknown13 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_14;
                array[15] = unknown13.ToString(CultureInfo.InvariantCulture);

                var unknown14 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_15;
                array[16] = unknown14.ToString(CultureInfo.InvariantCulture);

                var unknown15 = npcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_16;
                array[17] = unknown15.ToString(CultureInfo.InvariantCulture);

                gridCreatureGrps.Rows.Add(array);
                gridCreatureGrps.CellValueChanged -= ChangeCreatureGroups;
                gridCreatureGrps.Rows[i].HeaderCell.Value = NPCName(npcgen.creature_sets[e.RowIndex].creature_groups[i].id);
                gridCreatureGrps.CellValueChanged += ChangeCreatureGroups;
            }

            gridCreatureGrps.ContextMenuStrip = contextNPCGen;
        }

        private void ChangeResources(object sender, DataGridViewCellEventArgs e)
        {
            if (npcgen == null || gridResources.CurrentCell == null) return;

            try
            {
                var r = gridResources.CurrentCell.RowIndex;

                switch (gridResources.CurrentCell.ColumnIndex)
                {
                    case 1:
                        npcgen.resource_sets[r].spawn_x = Convert.ToSingle(gridResources.CurrentCell.Value);
                        break;

                    case 2:
                        npcgen.resource_sets[r].spawn_alt = Convert.ToSingle(gridResources.CurrentCell.Value);
                        break;

                    case 3:
                        npcgen.resource_sets[r].spawn_z = Convert.ToSingle(gridResources.CurrentCell.Value);
                        break;

                    case 4:
                        npcgen.resource_sets[r].spread_x = Convert.ToSingle(gridResources.CurrentCell.Value);
                        break;

                    case 5:
                        npcgen.resource_sets[r].spread_z = Convert.ToSingle(gridResources.CurrentCell.Value);
                        break;

                    case 6:
                        npcgen.resource_sets[r].unknown_1 = Convert.ToBoolean(gridResources.CurrentCell.Value);
                        break;

                    case 7:
                        npcgen.resource_sets[r].unknown_2 = Convert.ToBoolean(gridResources.CurrentCell.Value);
                        break;

                    case 8:
                        npcgen.resource_sets[r].unknown_3 = Convert.ToBoolean(gridResources.CurrentCell.Value);
                        break;

                    case 9:
                        npcgen.resource_sets[r].unknown_4 = Convert.ToInt32(gridResources.CurrentCell.Value);
                        break;

                    case 10:
                        npcgen.resource_sets[r].unknown_5 = Convert.ToByte(gridResources.CurrentCell.Value);
                        break;

                    case 11:
                        npcgen.resource_sets[r].unknown_6 = Convert.ToByte(gridResources.CurrentCell.Value);
                        break;

                    case 12:
                        npcgen.resource_sets[r].unknown_7 = Convert.ToByte(gridResources.CurrentCell.Value);
                        break;

                    case 13:
                        npcgen.resource_sets[r].unknown_trigger = Convert.ToInt32(gridResources.CurrentCell.Value);
                        break;

                    case 14:
                        npcgen.resource_sets[r].unknown_8 = Convert.ToInt32(gridResources.CurrentCell.Value);
                        break;

                    case 15:
                        npcgen.resource_sets[r].unknown_index = Convert.ToInt32(gridResources.CurrentCell.Value);
                        break;

                    case 16:
                        npcgen.resource_sets[r].unknown_mutual = Convert.ToInt32(gridResources.CurrentCell.Value);
                        break;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void ClickRowResource(object sender, DataGridViewCellEventArgs e)
        {
            gridResourceGrps.Rows.Clear();
            gridResourceGrps.ContextMenuStrip = null;

            if (npcgen == null || e.RowIndex <= -1 || e.RowIndex >= npcgen.resource_sets.Length) return;

            for (var i = 0; i < npcgen.resource_sets[e.RowIndex].resource_groups_count; i++)
            {
                var array = new object[5];
                var unknown = npcgen.resource_sets[e.RowIndex].resource_groups[i].unknown_1;
                array[0] = unknown.ToString(CultureInfo.InvariantCulture);

                var id = npcgen.resource_sets[e.RowIndex].resource_groups[i].id;
                array[1] = id.ToString(CultureInfo.InvariantCulture);

                var respawn = npcgen.resource_sets[e.RowIndex].resource_groups[i].respawn;
                array[2] = respawn.ToString(CultureInfo.InvariantCulture);

                var amount = npcgen.resource_sets[e.RowIndex].resource_groups[i].amount;
                array[3] = amount.ToString(CultureInfo.InvariantCulture);

                var unknown2 = npcgen.resource_sets[e.RowIndex].resource_groups[i].unknown_2;
                array[4] = unknown2.ToString(CultureInfo.InvariantCulture);

                gridResourceGrps.Rows.Add(array);
            }
            gridResourceGrps.ContextMenuStrip = contextNPCGen;
        }

        private void ChangeResourceGroups(object sender, DataGridViewCellEventArgs e)
        {
            if (npcgen == null || gridResources.CurrentCell == null ||
                gridResourceGrps.CurrentCell == null) return;

            try
            {
                var r = gridResources.CurrentCell.RowIndex;
                var n = gridResourceGrps.CurrentCell.RowIndex;

                switch (gridResourceGrps.CurrentCell.ColumnIndex)
                {
                    case 0:
                        npcgen.resource_sets[r].resource_groups[n].unknown_1 = Convert.ToInt32(gridResourceGrps.CurrentCell.Value);
                        break;

                    case 1:
                        npcgen.resource_sets[r].resource_groups[n].id = Convert.ToInt32(gridResourceGrps.CurrentCell.Value);

                        if (n == 0)
                        {
                            var id = npcgen.resource_sets[r].resource_groups[0].id;
                            gridResources.Rows[r].HeaderCell.Value = id.ToString(CultureInfo.InvariantCulture);
                        }
                        break;

                    case 2:
                        npcgen.resource_sets[r].resource_groups[n].respawn = Convert.ToInt32(gridResourceGrps.CurrentCell.Value);
                        break;

                    case 3:
                        npcgen.resource_sets[r].resource_groups[n].amount = Convert.ToInt32(gridResourceGrps.CurrentCell.Value);
                        break;

                    case 4:
                        npcgen.resource_sets[r].resource_groups[n].unknown_2 = Convert.ToInt32(gridResourceGrps.CurrentCell.Value);
                        break;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void ChangeDynamics(object sender, DataGridViewCellEventArgs e)
        {
            if (npcgen == null || gridDynamics.CurrentCell == null) return;

            try
            {
                var r = gridDynamics.CurrentCell.RowIndex;

                switch (gridDynamics.CurrentCell.ColumnIndex)
                {
                    case 0:
                        npcgen.dynamics[r].id = Convert.ToInt32(gridDynamics.CurrentCell.Value);
                        break;

                    case 1:
                        npcgen.dynamics[r].spawn_x = Convert.ToSingle(gridDynamics.CurrentCell.Value);
                        break;

                    case 2:
                        npcgen.dynamics[r].spawn_alt = Convert.ToSingle(gridDynamics.CurrentCell.Value);
                        break;

                    case 3:
                        npcgen.dynamics[r].spawn_z = Convert.ToSingle(gridDynamics.CurrentCell.Value);
                        break;

                    case 4:
                        npcgen.dynamics[r].unknown_5 = Convert.ToByte(gridDynamics.CurrentCell.Value);
                        break;

                    case 5:
                        npcgen.dynamics[r].unknown_6 = Convert.ToByte(gridDynamics.CurrentCell.Value);
                        break;

                    case 6:
                        npcgen.dynamics[r].unknown_7 = Convert.ToByte(gridDynamics.CurrentCell.Value);
                        break;

                    case 7:
                        npcgen.dynamics[r].unknown_8 = Convert.ToByte(gridDynamics.CurrentCell.Value);
                        break;

                    case 8:
                        npcgen.dynamics[r].trigger = Convert.ToInt32(gridDynamics.CurrentCell.Value);
                        break;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void ChangeTriggers(object sender, DataGridViewCellEventArgs e)
        {
            if (npcgen == null || gridTriggers.CurrentCell == null) return;

            try
            {
                var r = gridTriggers.CurrentCell.RowIndex;

                switch (gridTriggers.CurrentCell.ColumnIndex)
                {
                    case 0:
                        npcgen.triggers[r].unknown_1 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 1:
                        npcgen.triggers[r].unknown_2 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 2:
                        npcgen.triggers[r].name = Utils.GetBytesGBK(gridTriggers.CurrentCell.Value.ToString());
                        break;

                    case 3:
                        npcgen.triggers[r].unknown_3 = Convert.ToBoolean(gridTriggers.CurrentCell.Value);
                        break;

                    case 4:
                        npcgen.triggers[r].unknown_4 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 5:
                        npcgen.triggers[r].unknown_5 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 6:
                        npcgen.triggers[r].unknown_6 = Convert.ToBoolean(gridTriggers.CurrentCell.Value);
                        break;

                    case 7:
                        npcgen.triggers[r].unknown_7 = Convert.ToBoolean(gridTriggers.CurrentCell.Value);
                        break;

                    case 8:
                        npcgen.triggers[r].year_1 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 9:
                        npcgen.triggers[r].month_1 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 10:
                        npcgen.triggers[r].week_day_1 = Column44.Items.IndexOf(gridTriggers.CurrentCell.Value) - 1;
                        break;

                    case 11:
                        npcgen.triggers[r].day_1 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 12:
                        npcgen.triggers[r].hour_1 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 13:
                        npcgen.triggers[r].minute_1 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 14:
                        npcgen.triggers[r].year_2 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 15:
                        npcgen.triggers[r].month_2 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 16:
                        npcgen.triggers[r].week_day_2 = Column48.Items.IndexOf(gridTriggers.CurrentCell.Value) - 1;
                        break;

                    case 17:
                        npcgen.triggers[r].day_2 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 18:
                        npcgen.triggers[r].hour_2 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 19:
                        npcgen.triggers[r].minute_2 = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 20:
                        npcgen.triggers[r].duration = Convert.ToInt32(gridTriggers.CurrentCell.Value);
                        break;

                    case 21:
                        npcgen.triggers[r].forced_stop = Convert.ToBoolean(gridTriggers.CurrentCell.Value);
                        break;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void AddRowClick(object sender, EventArgs e)
        {
            if (npcgen == null) return;

            if (contextNPCGen.SourceControl == gridCreatures)
            {
                npcgen.creature_sets_count = npcgen.creature_sets_count + 1;
                Array.Resize(ref npcgen.creature_sets, npcgen.creature_sets.Length + 1);
                npcgen.creature_sets[npcgen.creature_sets.Length - 1] = new CreatureSets();

                npcgen.creature_sets[npcgen.creature_sets.Length - 1].spawn_mode = 0;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].creature_groups_count = 0;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].spawn_x = 0f;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].spawn_alt = 0f;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].spawn_z = 0f;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].rot_1 = 0f;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].rot_2 = 0f;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].rot_3 = 0f;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].spread_x = 0f;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].spread_alt = 0f;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].spread_z = 0f;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].unknown_7 = 0;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].unknown_8 = 0;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].unknown_9 = true;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].unknown_10 = true;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].unknown_11 = true;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].unknown_12 = 0;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].trigger = 0;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].unknown_14 = 0;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].unknown_15 = 0;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].unknown_16 = 0;
                npcgen.creature_sets[npcgen.creature_sets.Length - 1].unknown_17 = 0;

                npcgen.creature_sets[npcgen.creature_sets.Length - 1].creature_groups = new CreatureGroup[0];

                gridCreatures.Rows.Add("0", Column2.Items[0].ToString(), "0", "0", "0", "0", "0", "0", "0", "0", "0",
                    Column12.Items[0].ToString(), "0", "True", "True", "True", "0", "0", "0", "0", "0", "0");
                gridCreatures.Rows[gridCreatures.Rows.Count - 1].HeaderCell.Value = "";
            }

            if (contextNPCGen.SourceControl == gridCreatureGrps)
            {
                var r = gridCreatures.CurrentCell.RowIndex;

                npcgen.creature_sets[r].creature_groups_count = npcgen.creature_sets[r].creature_groups_count + 1;
                Array.Resize(ref npcgen.creature_sets[r].creature_groups, npcgen.creature_sets[r].creature_groups.Length + 1);
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1] = new CreatureGroup();

                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].id = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].amount = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].respawn = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_2 = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_3 = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_4 = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_5 = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_6 = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_7 = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_8 = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_9 = false;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_10 = true;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_11 = true;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_12 = true;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_13 = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_14 = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_15 = 0;
                npcgen.creature_sets[r].creature_groups[npcgen.creature_sets[r].creature_groups.Length - 1].unknown_16 = 0;

                gridCreatureGrps.Rows.Add("0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "False",
                    "True", "True", "True", "0", "0", "0", "0");

                if (npcgen.creature_sets[r].creature_groups.Length > 0)
                {
                    var id = npcgen.creature_sets[r].creature_groups[0].id;
                    gridCreatures.Rows[r].HeaderCell.Value = "[" + id.ToString(CultureInfo.InvariantCulture) + "] " +
                        NPCName(npcgen.creature_sets[r].creature_groups[0].id);
                }
                else
                {
                    gridCreatures.Rows[r].HeaderCell.Value = "";
                }

                gridCreatures.Rows[r].Cells[0].Value =
                    gridCreatureGrps.Rows.Count.ToString(CultureInfo.InvariantCulture);
            }

            if (contextNPCGen.SourceControl == gridResources)
            {
                npcgen.resource_sets_count = npcgen.resource_sets_count + 1;
                Array.Resize(ref npcgen.resource_sets, npcgen.resource_sets.Length + 1);
                npcgen.resource_sets[npcgen.resource_sets.Length - 1] = new ResourceSets();

                npcgen.resource_sets[npcgen.resource_sets.Length - 1].spawn_x = 0f;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].spawn_alt = 0f;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].spawn_z = 0f;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].spread_x = 0f;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].spread_z = 0f;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].resource_groups_count = 0;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_1 = true;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_2 = true;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_3 = true;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_4 = 0;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_5 = 192;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_6 = 63;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_7 = 0;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_trigger = 0;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_8 = 0;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_index = 0;
                npcgen.resource_sets[npcgen.resource_sets.Length - 1].unknown_mutual = 0;

                npcgen.resource_sets[npcgen.resource_sets.Length - 1].resource_groups = new ResourceGroup[0];

                gridResources.Rows.Add("0", "0", "0", "0", "0", "0", "True",
                    "True", "True", "0", "192", "63", "0", "0", "0", "0", "0");
                gridResources.Rows[gridResources.Rows.Count - 1].HeaderCell.Value = "";
            }

            if (contextNPCGen.SourceControl == gridResourceGrps)
            {
                var i = gridResources.CurrentCell.RowIndex;

                npcgen.resource_sets[i].resource_groups_count = npcgen.resource_sets[i].resource_groups_count + 1;
                Array.Resize(ref npcgen.resource_sets[i].resource_groups, npcgen.resource_sets[i].resource_groups.Length + 1);
                npcgen.resource_sets[i].resource_groups[npcgen.resource_sets[i].resource_groups.Length - 1] = new ResourceGroup();

                npcgen.resource_sets[i].resource_groups[npcgen.resource_sets[i].resource_groups.Length - 1].unknown_1 = 0;
                npcgen.resource_sets[i].resource_groups[npcgen.resource_sets[i].resource_groups.Length - 1].id = 0;
                npcgen.resource_sets[i].resource_groups[npcgen.resource_sets[i].resource_groups.Length - 1].respawn = 0;
                npcgen.resource_sets[i].resource_groups[npcgen.resource_sets[i].resource_groups.Length - 1].amount = 0;
                npcgen.resource_sets[i].resource_groups[npcgen.resource_sets[i].resource_groups.Length - 1].unknown_2 = 0;

                gridResourceGrps.Rows.Add("0", "0", "0", "0", "0");

                gridResources.Rows[i].HeaderCell.Value =
                    npcgen.resource_sets[i].resource_groups.Length > 0 ?
                    npcgen.resource_sets[i].resource_groups[0].id.ToString(CultureInfo.InvariantCulture) : "";

                gridResources.Rows[i].Cells[0].Value =
                    (gridResourceGrps.Rows.Count).ToString(CultureInfo.InvariantCulture);
            }

            if (contextNPCGen.SourceControl == gridDynamics)
            {
                npcgen.dynamics_count = npcgen.dynamics_count + 1;
                Array.Resize(ref npcgen.dynamics, npcgen.dynamics.Length + 1);
                npcgen.dynamics[npcgen.dynamics.Length - 1] = new Dynamic();

                npcgen.dynamics[npcgen.dynamics.Length - 1].id = 0;
                npcgen.dynamics[npcgen.dynamics.Length - 1].spawn_x = 0f;
                npcgen.dynamics[npcgen.dynamics.Length - 1].spawn_alt = 0f;
                npcgen.dynamics[npcgen.dynamics.Length - 1].spawn_z = 0f;
                npcgen.dynamics[npcgen.dynamics.Length - 1].unknown_5 = 0;
                npcgen.dynamics[npcgen.dynamics.Length - 1].unknown_6 = 0;
                npcgen.dynamics[npcgen.dynamics.Length - 1].unknown_7 = 0;
                npcgen.dynamics[npcgen.dynamics.Length - 1].unknown_8 = 16;
                npcgen.dynamics[npcgen.dynamics.Length - 1].trigger = 0;

                gridDynamics.Rows.Add("0", "0", "0", "0", "0", "0", "0", "16", "0");
                gridDynamics.Rows[gridDynamics.Rows.Count - 1].HeaderCell.Value =
                    (gridDynamics.Rows.Count - 1).ToString(CultureInfo.InvariantCulture);
            }

            if (contextNPCGen.SourceControl != gridTriggers) return;

            npcgen.triggers_count = npcgen.triggers_count + 1;
            Array.Resize(ref npcgen.triggers, npcgen.triggers.Length + 1);
            npcgen.triggers[npcgen.triggers.Length - 1] = new Trigger();

            npcgen.triggers[npcgen.triggers.Length - 1].unknown_1 = 0;
            npcgen.triggers[npcgen.triggers.Length - 1].unknown_2 = 0;
            npcgen.triggers[npcgen.triggers.Length - 1].name = new byte[128];
            npcgen.triggers[npcgen.triggers.Length - 1].unknown_3 = false;
            npcgen.triggers[npcgen.triggers.Length - 1].unknown_4 = 0;
            npcgen.triggers[npcgen.triggers.Length - 1].unknown_5 = 0;
            npcgen.triggers[npcgen.triggers.Length - 1].unknown_6 = false;
            npcgen.triggers[npcgen.triggers.Length - 1].unknown_7 = false;
            npcgen.triggers[npcgen.triggers.Length - 1].year_1 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].month_1 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].week_day_1 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].day_1 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].hour_1 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].minute_1 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].year_2 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].month_2 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].week_day_2 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].day_2 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].hour_2 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].minute_2 = -1;
            npcgen.triggers[npcgen.triggers.Length - 1].duration = 0;
            npcgen.triggers[npcgen.triggers.Length - 1].forced_stop = false;

            gridTriggers.Rows.Add("0", "0", "", "False", "0", "0", "True", "True", "-1", "-1",
                Column44.Items[0].ToString(), "-1", "-1", "0", "-1", "-1", Column48.Items[0].ToString(),
                "-1", "-1", "0", "60", "False");

            gridTriggers.Rows[gridTriggers.Rows.Count - 1].HeaderCell.Value =
                (gridTriggers.Rows.Count - 1).ToString(CultureInfo.InvariantCulture);
        }

        private void DeleteRowClick(object sender, EventArgs e)
        {
            if (npcgen == null) return;

            if (contextNPCGen.SourceControl == gridCreatures)
            {
                if (npcgen.creature_sets_count > 0 && gridCreatures.CurrentCell.RowIndex > -1)
                {
                    npcgen.creature_sets_count--;

                    var tempCS = new CreatureSets[npcgen.creature_sets_count];
                    Array.Copy(npcgen.creature_sets, 0, tempCS, 0, gridCreatures.CurrentCell.RowIndex);
                    Array.Copy(npcgen.creature_sets, gridCreatures.CurrentCell.RowIndex + 1, tempCS,
                        gridCreatures.CurrentCell.RowIndex, npcgen.creature_sets_count - gridCreatures.CurrentCell.RowIndex);
                    npcgen.creature_sets = tempCS;

                    gridCreatureGrps.Rows.Clear();
                    gridCreatures.Rows.RemoveAt(gridCreatures.CurrentCell.RowIndex);
                }
            }

            if (contextNPCGen.SourceControl == gridCreatureGrps)
            {
                if (gridCreatures.CurrentCell.RowIndex > -1)
                {
                    var r = gridCreatures.CurrentCell.RowIndex;

                    if (npcgen.creature_sets[r].creature_groups_count > 0 && gridCreatureGrps.CurrentCell.RowIndex > -1)
                    {
                        npcgen.creature_sets[r].creature_groups_count--;

                        var tempCG = new CreatureGroup[npcgen.creature_sets[r].creature_groups_count];
                        Array.Copy(npcgen.creature_sets[r].creature_groups, 0, tempCG, 0, gridCreatureGrps.CurrentCell.RowIndex);
                        Array.Copy(npcgen.creature_sets[r].creature_groups, gridCreatureGrps.CurrentCell.RowIndex + 1, tempCG,
                            gridCreatureGrps.CurrentCell.RowIndex,
                            npcgen.creature_sets[r].creature_groups_count - gridCreatureGrps.CurrentCell.RowIndex);
                        npcgen.creature_sets[r].creature_groups = tempCG;

                        gridCreatureGrps.Rows.RemoveAt(gridCreatureGrps.CurrentCell.RowIndex);

                        if (npcgen.creature_sets[r].creature_groups.Length > 0)
                        {
                            gridCreatures.Rows[r].HeaderCell.Value =
                                "[" + npcgen.creature_sets[r].creature_groups[0].id.ToString(CultureInfo.InvariantCulture) + "] " +
                                NPCName(npcgen.creature_sets[r].creature_groups[0].id);
                        }
                        else
                        {
                            gridCreatures.Rows[r].HeaderCell.Value = "";
                        }

                        gridCreatures.Rows[r].Cells[0].Value =
                            (gridCreatureGrps.Rows.Count).ToString(CultureInfo.InvariantCulture);
                    }
                }
            }

            if (contextNPCGen.SourceControl == gridResources)
            {
                if (npcgen.resource_sets_count > 0 && gridResources.CurrentCell.RowIndex > -1)
                {
                    npcgen.resource_sets_count--;

                    var tempRS = new ResourceSets[npcgen.resource_sets_count];
                    Array.Copy(npcgen.resource_sets, 0, tempRS, 0, gridResources.CurrentCell.RowIndex);
                    Array.Copy(npcgen.resource_sets, gridResources.CurrentCell.RowIndex + 1, tempRS,
                        gridResources.CurrentCell.RowIndex, npcgen.resource_sets_count - gridResources.CurrentCell.RowIndex);
                    npcgen.resource_sets = tempRS;

                    gridResourceGrps.Rows.Clear();
                    gridResources.Rows.RemoveAt(gridResources.CurrentCell.RowIndex);
                }
            }

            if (contextNPCGen.SourceControl == gridResourceGrps)
            {
                if (gridResources.CurrentCell.RowIndex > -1)
                {
                    var r = gridResources.CurrentCell.RowIndex;

                    if (npcgen.resource_sets[r].resource_groups_count > 0 && gridResourceGrps.CurrentCell.RowIndex > -1)
                    {
                        npcgen.resource_sets[r].resource_groups_count--;

                        var tempRG = new ResourceGroup[npcgen.resource_sets[r].resource_groups_count];
                        Array.Copy(npcgen.resource_sets[r].resource_groups, 0, tempRG, 0, gridResourceGrps.CurrentCell.RowIndex);
                        Array.Copy(npcgen.resource_sets[r].resource_groups, gridResourceGrps.CurrentCell.RowIndex + 1, tempRG,
                            gridResourceGrps.CurrentCell.RowIndex,
                            npcgen.resource_sets[r].resource_groups_count - gridResourceGrps.CurrentCell.RowIndex);
                        npcgen.resource_sets[r].resource_groups = tempRG;

                        gridResourceGrps.Rows.RemoveAt(gridResourceGrps.CurrentCell.RowIndex);
                        gridResources.Rows[r].HeaderCell.Value = npcgen.resource_sets[r].resource_groups.Length > 0 ?
                            npcgen.resource_sets[r].resource_groups[0].id.ToString(CultureInfo.InvariantCulture) : "";
                        gridResources.Rows[r].Cells[0].Value = (gridResourceGrps.Rows.Count).ToString(CultureInfo.InvariantCulture);
                    }
                }
            }

            if (contextNPCGen.SourceControl == gridDynamics)
            {
                if (npcgen.dynamics_count > 0 && gridDynamics.CurrentCell.RowIndex > -1)
                {
                    npcgen.dynamics_count--;

                    var tempD = new Dynamic[npcgen.dynamics_count];
                    Array.Copy(npcgen.dynamics, 0, tempD, 0, gridDynamics.CurrentCell.RowIndex);
                    Array.Copy(npcgen.dynamics, gridDynamics.CurrentCell.RowIndex + 1, tempD,
                        gridDynamics.CurrentCell.RowIndex, npcgen.dynamics_count - gridDynamics.CurrentCell.RowIndex);
                    npcgen.dynamics = tempD;

                    gridDynamics.Rows.RemoveAt(gridDynamics.CurrentCell.RowIndex);
                }
            }

            if (contextNPCGen.SourceControl != gridTriggers) return;

            if (npcgen.triggers_count <= 0 || gridTriggers.CurrentCell.RowIndex <= -1) return;
            npcgen.triggers_count--;

            var tempT = new Trigger[npcgen.triggers_count];
            Array.Copy(npcgen.triggers, 0, tempT, 0, gridTriggers.CurrentCell.RowIndex);
            Array.Copy(npcgen.triggers, gridTriggers.CurrentCell.RowIndex + 1, tempT,
                       gridTriggers.CurrentCell.RowIndex, npcgen.triggers_count - gridTriggers.CurrentCell.RowIndex);
            npcgen.triggers = tempT;

            gridTriggers.Rows.RemoveAt(gridTriggers.CurrentCell.RowIndex);
        }

        private void OpenNPCGen()
        {
            var openNPC = new OpenFileDialog();
            openNPC.Filter = Resources.NPCGenFilter;
            openNPC.Title = Resources.NPCGenTitle;
            openNPC.FileName = "npcgen.data";

            if (openNPC.ShowDialog() == DialogResult.OK && File.Exists(openNPC.FileName))
            {
                try
                {
                    Cursor = Cursors.AppStarting;
                    progressBar.Style = ProgressBarStyle.Blocks;

                    var npcStream = File.OpenRead(openNPC.FileName);
                    var npcReader = new BinaryReader(npcStream);

                    npcgen = new NPCGen();
                    npcgen.version = npcReader.ReadInt32();
                    npcgen.creature_sets_count = npcReader.ReadInt32();
                    npcgen.resource_sets_count = npcReader.ReadInt32();
                    npcgen.dynamics_count = npcReader.ReadInt32();
                    npcgen.triggers_count = npcReader.ReadInt32();

                    JDLoadCreatures(npcReader);
                    JDLoadResources(npcReader);
                    JDLoadDynamics(npcReader);
                    JDLoadTriggers(npcReader);

                    npcReader.Close();
                    npcStream.Close();

                    Text = Resources.RaGEEditor + Resources.Version + Utils.Version + Resources.LSParen + openNPC.FileName + Resources.RPAREN;
                    progressBar.Style = ProgressBarStyle.Continuous;
                    Cursor = Cursors.Default;
                }
                catch (Exception npcEx)
                {
                    MessageBox.Show(Resources.ErrLoadNPC + npcEx.Message);
                    progressBar.Style = ProgressBarStyle.Continuous;
                    Cursor = Cursors.Default;
                }
            }
        }

        private void SaveNPCGen()
        {
            var save = new SaveFileDialog();
            save.InitialDirectory = Environment.CurrentDirectory;
            save.Filter = Resources.NPCGenFilter;
            save.FileName = "npcgen.data";

            if (save.ShowDialog() == DialogResult.OK && save.FileName != "")
            {
                try
                {
                    Cursor = Cursors.AppStarting;
                    progressBar.Style = ProgressBarStyle.Blocks;

                    var fs = new FileStream(save.FileName, FileMode.Create, FileAccess.Write);
                    var bw = new BinaryWriter(fs);

                    bw.Write(npcgen.version); //npcgen.version
                    bw.Write(npcgen.creature_sets_count);
                    bw.Write(npcgen.resource_sets_count);
                    bw.Write(npcgen.dynamics_count);
                    bw.Write(npcgen.triggers_count);

                    SaveCreatures(bw);
                    SaveResources(bw);
                    SaveDynamics(bw);
                    SaveTriggers(bw);

                    bw.Close();
                    fs.Close();

                    progressBar.Style = ProgressBarStyle.Continuous;
                    Cursor = Cursors.Default;
                    MessageBox.Show(Resources.FileSaved);
                }
                catch
                {
                    MessageBox.Show(Resources.ErrSaveNPC);
                    progressBar.Style = ProgressBarStyle.Continuous;
                    Cursor = Cursors.Default;
                }
            }
        }
    }
}
